home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 247_02 / palin.c < prev    next >
Text File  |  1989-04-17  |  1KB  |  52 lines

  1. /*
  2.  *   Program to investigate palindromic reversals.
  3.  *   Gruenberger F. Computer Recreations, Scientific American. April 1984.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "miracl.h"
  8.  
  9. bool reverse(x,y)
  10. big x;
  11. big y;
  12. { /* reverse digits of x into y       *
  13.    * returns TRUE if x is palindromic */
  14.     int m,n;
  15.     int i,k;
  16.     bool palin;
  17.     copy(x,y);
  18.     palin=TRUE;
  19.     k=numdig(y)+1;
  20.     for (i=1;i<=(numdig(x)+1)/2;i++)
  21.     {
  22.         k--;
  23.         m=getdig(x,k);
  24.         n=getdig(x,i);
  25.         if (m!=n) palin=FALSE;
  26.         putdig(m,x,i);
  27.         putdig(n,x,k);
  28.     }
  29.     return palin;
  30. }
  31.  
  32. main()
  33. {  /*  palindromic reversals  */
  34.     int iter;
  35.     big x,y;
  36.     mirsys(100,10);
  37.     x=mirvar(0);
  38.     y=mirvar(0);
  39.     printf("palindromic reversals program\n");
  40.     printf("input number\n");
  41.     innum(x,stdin);
  42.     iter=0;
  43.     while (!reverse(x,y))
  44.     {
  45.         iter++;
  46.         add(x,y,x);
  47.         otnum(x,stdout);
  48.     }
  49.     printf("palindromic after %d iterations\n",iter);
  50. }
  51.  
  52.